package com.dao;

import java.sql.Timestamp;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.base.BaseDao;
import com.bean.TransactionRecord;
import com.util.GUtil;

/**
 * A data access object (DAO) providing persistence and search support for TransactionRecord entities.
 * Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
 * @see com.bean.TransactionRecord
 * @author MyEclipse Persistence Tools 
 */
public class TransactionRecordDao extends BaseDao  {
	private static final Logger log = Logger.getLogger(TransactionRecordDao.class);
	//property constants
	public static final String PAY_TYPE = "payType";
	public static final String CATEGORY = "category";
	public static final String AMOUNT = "amount";
	public static final String STATUS = "status";


	public void save(TransactionRecord transientInstance) {
		log.debug("saving TransactionRecord instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public String delete(TransactionRecord persistentInstance, Integer flag) {
		log.debug("deleting TransactionRecord instance");
		try {
			if(flag==null)
				getSession().delete(persistentInstance);
			else{
				persistentInstance.setStatus(flag.shortValue());
				getSession().update(persistentInstance);
			}
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
		return GUtil.SUCCESS;
	}

	public TransactionRecord findById( java.lang.Long id) {
		log.debug("getting TransactionRecord instance with id: " + id);
		try {
			TransactionRecord instance = (TransactionRecord) getSession()
					.get("com.dao.TransactionRecord", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}


	public List findByExample(TransactionRecord instance) {
		log.debug("finding TransactionRecord instance by example");
		try {
			List results = getSession()
					.createCriteria("com.dao.TransactionRecord")
					.add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}    

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TransactionRecord instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TransactionRecord as model where model." 
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPayType(Object payType
			) {
		return findByProperty(PAY_TYPE, payType
				);
	}

	public List findByCategory(Object category
			) {
		return findByProperty(CATEGORY, category
				);
	}

	public List findByAmount(Object amount
			) {
		return findByProperty(AMOUNT, amount
				);
	}

	public List findByStatus(Object status
			) {
		return findByProperty(STATUS, status
				);
	}


	public List findAll() {
		log.debug("finding all TransactionRecord instances");
		try {
			String queryString = "from TransactionRecord";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TransactionRecord merge(TransactionRecord detachedInstance) {
		log.debug("merging TransactionRecord instance");
		try {
			TransactionRecord result = (TransactionRecord) getSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TransactionRecord instance) {
		log.debug("attaching dirty TransactionRecord instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TransactionRecord instance) {
		log.debug("attaching clean TransactionRecord instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}